import sqlite3


class DbHandler():
    __data_name = ''

    def open(self, data_name):
        self.__data_name = data_name;
        self.__conn = sqlite3.connect(data_name)
        self.__c = self.__conn.cursor()

    def close(self):
        self.__conn.commit()
        self.__conn.close()

    def dict_ball(self, cycle):
        dict_return = {}
        cursor = self.__c.execute('SELECT POSX,POSY,VELX,VELY FROM RCDATA \
                                 WHERE OBJECT = 0 AND CYCLE = ' + str(cycle))
        for row in cursor:
            dict_return['posx'] = row[0]
            dict_return['posy'] = row[1]
            dict_return['velx'] = row[2]
            dict_return['vely'] = row[3]
        return dict_return

    def dict_player(self, cycle, object):
        dict_return = {}
        cursor = self.__c.execute('SELECT POSX,POSY,VELX,VELY,BODYDIR,STAMINA FROM RCDATA \
                                 WHERE OBJECT = ' + str(object) +
                                ' AND CYCLE = ' + str(cycle))
        for row in cursor:
            dict_return['posx'] = row[0]
            dict_return['posy'] = row[1]
            dict_return['velx'] = row[2]
            dict_return['vely'] = row[3]
            dict_return['bodydir'] = row[4]
            dict_return['stamina'] = row[5]
        return dict_return
